<?

if ( function_exists('register_sidebar') ) {
  register_sidebar(array(
    'name' => 'Right Side',
    'id' => 'right',
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h2 class="widgettitle">',
    'after_title' => '</h2>'
  ));
  register_sidebar(array(
    'name' => 'Bottom',
    'id' => 'foot',
    'before_widget' => '<li id="%1$s" class="foot-widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h2 class="widgettitle">',
    'after_title' => '</h2>'
  ));
}

function time_since_last_comment() {

  if ($post->comment_count > 0) { 
    echo ("<p>Last comment was posted");

    if (function_exists('time_since')) { 
      echo time_since(abs(strtotime(last_comment_date($post->ID) . " GMT")), time());
      echo (" ago ");
    } else { 
      the_time('F jS, Y'); 
    } 

    echo ("</p>");

  }
}

/* 
Function Name: Time Since 
Function URI: http://binarybonsai.com/wordpress/timesince
Description: Tells the time between the entry being posted and the comment being made.
Author: Michael Heilemann & Dunstan Orchard
Author URI: http://binarybonsai.com
*/
function time_since($older_date, $newer_date = false)
{
	// array of time period chunks
	$chunks = array(
	array(60 * 60 * 24 * 365 , 'year'),
	array(60 * 60 * 24 * 30 , 'month'),
	array(60 * 60 * 24 * 7, 'week'),
	array(60 * 60 * 24 , 'day'),
	array(60 * 60 , 'hour'),
	array(60 , 'minute'),
	);
	
	// $newer_date will equal false if we want to know the time elapsed between a date and the current time
	// $newer_date will have a value if we want to work out time elapsed between two known dates
	$newer_date = ($newer_date == false) ? (time()+(60*60*get_settings("gmt_offset"))) : $newer_date;
	
	// difference in seconds
	$since = $newer_date - $older_date;
	
	// we only want to output two chunks of time here, eg:
	// x years, xx months
	// x days, xx hours
	// so there's only two bits of calculation below:

	// step one: the first chunk
	for ($i = 0, $j = count($chunks); $i < $j; $i++)
		{
		$seconds = $chunks[$i][0];
		$name = $chunks[$i][1];

		// finding the biggest chunk (if the chunk fits, break)
		if (($count = floor($since / $seconds)) != 0)
			{
			break;
			}
		}

	// set output var
	$output = ($count == 1) ? '1 '.$name : "$count {$name}s";

	// step two: the second chunk
	if ($i + 1 < $j)
		{
		$seconds2 = $chunks[$i + 1][0];
		$name2 = $chunks[$i + 1][1];
		
		if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0)
			{
			// add to output var
			$output .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
			}
		}
	
	return $output;
}

// Get the date of the last comment for a particular post.
function last_comment_date($post_id) {
	global $wpdb;
	
	$date = $wpdb->get_results("
		SELECT comment_date_gmt FROM `$wpdb->comments`
		WHERE comment_post_id = $post_id
		ORDER BY comment_date_gmt DESC
		LIMIT 1", ARRAY_N);

	return $date[0][0];
}




function time_since_start() {
	global $wpdb;
	if ( !$time_since_start = wp_cache_get( 'time_since_start', 'function' ) ) {
		$tss = $wpdb->get_results( "SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 1" );
		
		$time_since_start = $tss[0]->post_date;
		wp_cache_add( 'time_since_start', $time_since_start, 'function' );
	}

	if ( function_exists('time_since') ) { return time_since( abs( strtotime( $time_since_start ) . " GMT" ), time() ); }
	return $time_since_start;
}


?>
